import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class InsertPart extends JFrame 
{
        ArrayList<String> values = new ArrayList<String>();// The selected values.
        private String selected;


        private JTextField displayField = new JTextField(30);

        private JComboBox c = new JComboBox();

        private JButton b = new JButton("Add item");

        private int count = 0;

        public InsertPart(final Connection connection, final ConnectDb connectDb) 
        {
            setLayout(new FlowLayout());

                //-------------------------------------------------------------------
                final queryVendorPart q = new queryVendorPart(connection, connectDb);
               
                
        
                 final ArrayList<String> temp = q.queryMaker();
                 final int x = temp.size();

                 for(int i = 0;i<x;i++)
                     c.addItem(temp.get(i));
                        
                displayField.setEditable(false);
                
             
                c.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                        count++;
                                        selected = temp.get(c.getSelectedIndex());

                        }
                });
                final JTextField pnum = new JTextField("Part num");
                final JTextField tmount = new JTextField("TMount");
                final JTextField core = new JTextField("Core");
                final JTextField  inhead = new JTextField("InHead");
                final JTextField outhead = new JTextField("OutHead");
                final JTextField incon = new JTextField("InCon");
                final JTextField oucon = new JTextField("OuCon");
                final JTextField oilcool = new JTextField("OilCool");
                final JTextField price = new JTextField("Price");
                final JTextField amount = new JTextField("Amnt");
                
                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {    
                            //c.removeAllItems();
                    ArrayList<String> params = new ArrayList<String>();
                    
                     params.add(pnum.getText());
                    params.add(core.getText());
                    params.add(inhead.getText());
                    params.add(outhead.getText());
                    params.add(incon.getText());
                    params.add(oucon.getText());
                    params.add(tmount.getText());
                    params.add(oilcool.getText());
                    params.add(price.getText());
                    params.add(amount.getText());
                    
                    String query = "SELECT TNAME FROM MAKER WHERE MAKE= '" + selected+"'";
                    String tableName = "";
                    System.out.println(query);
                    try
                    {
                        Statement stmnt2 = connection.createStatement();
                        ResultSet rs = stmnt2.executeQuery(query);
                        
                        while(rs.next())
                            tableName = rs.getString(1);
                    }
                    
                    catch (SQLException ex) 
                    {
                    System.err.println(ex.getMessage());
                    }
                    String values = "";
                    
                    for(int i = 0; i < params.size(); i++){
                        
                        if(i==params.size()-1){
                                values += "'" + params.get(i).trim()+ "'";
                        }
                        else{
                        values += "'" + params.get(i).trim()+ "',";
                        } 
                    }

                    String query2 = "Insert into " + tableName + " values ("+ values+")";
                    try
                    {
                        System.out.println(query2);
                        Statement stmnt2 = connection.createStatement();
                        int suc = stmnt2.executeUpdate(query2);
                        if(suc >0){
                            System.out.println("Updated sucess");
                        }
                    }
                    
                    catch (SQLException ex) 
                    {
                    System.err.println(ex.getMessage());
                    }

                    }
            });
                JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.
                JPanel text = new JPanel();

                optionPanel.setLayout(new FlowLayout());
                text.setLayout(new FlowLayout());
                //displayPanel.add(displayField);
                text.add(pnum);
                text.add(core);
                text.add(inhead);
                text.add(outhead);
                text.add(incon);
                text.add(oucon);
                text.add(tmount);
                text.add(oilcool);
                text.add(price);
                text.add(amount);
                optionPanel.add(c);
                optionPanel.add(b);

                JFrame frame = new JFrame();
                frame.setLayout(new BorderLayout());
                //frame.add(displayPanel,BorderLayout.NORTH);
                frame.add(optionPanel,BorderLayout.NORTH);
                frame.add(text, BorderLayout.CENTER);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setVisible(true);

        }
        
        
}

